home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Draw / Sources / PaletteFacet.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  5.2 KB  |  223 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                PaletteFacet.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef PALETTEFACET_H
  14. #include "PaletteFacet.h"
  15. #endif
  16.  
  17. #ifndef DRAWPART_H
  18. #include "DrawPart.h"
  19. #endif
  20.  
  21. #ifndef DRAWSELECTION_H
  22. #include "DrawSelection.h"
  23. #endif
  24.  
  25. #ifndef SHAPES_H
  26. #include "Shapes.h"
  27. #endif
  28.  
  29. #ifndef DRAWFRAME_H
  30. #include "DrawFrame.h"
  31. #endif
  32.  
  33. // ----- Framework Includes -----
  34.  
  35. #ifndef FWMEMMGR_H
  36. #include <FWMemMgr.h>
  37. #endif
  38.  
  39. #ifndef FWUTIL_H
  40. #include "FWUtil.h"
  41. #endif
  42.  
  43. #ifndef FWFRMING_H
  44. #include "FWFrming.h"
  45. #endif
  46.  
  47. // ----- Graphix Includes -----
  48.  
  49. #ifndef FWINK_H
  50. #include "FWInk.h"
  51. #endif
  52.  
  53. #ifndef FWSTYLE_H
  54. #include "FWStyle.h"
  55. #endif
  56.  
  57. #ifndef FWCOLOR_H
  58. #include "FWColor.h"
  59. #endif
  60.  
  61. // ----- OpenDoc Includes -----
  62.  
  63. #ifndef _TRNSFORM_
  64. #include <Trnsform.h>
  65. #endif
  66.  
  67. // ----- Macintosh Includes -----
  68.  
  69. #if defined(FW_BUILD_MAC) && !defined(__EVENTS__)
  70. #include <Events.h>        // GetMouse, StillDown.
  71. #endif
  72.  
  73. #if defined(FW_BUILD_MAC) && !defined(__TOOLUTILS__)
  74. #include <ToolUtils.h>    // HiWord etc.
  75. #endif
  76.  
  77. #if defined(FW_BUILD_MAC) && !defined(__LIMITS__)
  78. #include <limits.h>
  79. #endif
  80.  
  81. #if defined(FW_BUILD_MAC) && !defined(__STDLIB__)
  82. #include <StdLib.h>        // Abs
  83. #endif
  84.  
  85. #if defined(FW_BUILD_MAC) && !defined(__DRAG__)
  86. #include <Drag.h>
  87. #endif
  88.  
  89. #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
  90. #include <math routines.h>        // See MoveTransformBy
  91. #endif
  92.  
  93. #pragma segment drawpart
  94.  
  95. //=========================================================================
  96. // CPaletteFacet
  97. //=========================================================================
  98.  
  99. //------------------------------------------------------------------------------
  100. // CPaletteFacet::CPaletteFacet
  101. //------------------------------------------------------------------------------
  102.  
  103. CPaletteFacet::CPaletteFacet() :
  104.     fColorTable(NULL)
  105. {
  106. }
  107.  
  108. //------------------------------------------------------------------------------
  109. // CPaletteFacet::InitPaletteFacet
  110. //------------------------------------------------------------------------------
  111.  
  112. void CPaletteFacet::InitPaletteFacet(XMPFacet* xmpFacet, CDrawPart* drawPart)
  113. {
  114.     InitFacet(xmpFacet);
  115.     fDrawPart = drawPart;
  116.     
  117.     fColorTable = ::GetCTable(8);
  118. }
  119.  
  120. //------------------------------------------------------------------------------
  121. // CPaletteFacet::~CPaletteFacet
  122. //------------------------------------------------------------------------------
  123.  
  124. CPaletteFacet::~CPaletteFacet()
  125. {
  126.     if (fColorTable)
  127.         ::DisposCTable(fColorTable);
  128. }
  129.  
  130. //------------------------------------------------------------------------------
  131. // CPaletteFacet::Draw
  132. //------------------------------------------------------------------------------
  133.  
  134. void CPaletteFacet::Draw(FW_CGraphicContext *gc)
  135. {    
  136.     // ----- Draw the grid -----
  137.     FW_CLineShape line(FW_CPoint(ff(2), ff(2)), FW_CPoint(ff(kPaletteWindowWidth-3), ff(2)));
  138.     for (unsigned short i = 1; i<=9; i++)
  139.     {
  140.         line->Draw(gc);
  141.         line->MoveShape(0, ff(9));
  142.     }
  143.  
  144.     line->SetLineStart(FW_CPoint(ff(2), ff(2)));
  145.     line->SetLineEnd(FW_CPoint(ff(2), ff(kPaletteWindowHeight-3)));
  146.     for (i = 1; i<=33; i++)
  147.     {
  148.         line->Draw(gc);
  149.         line->MoveShape(ff(9), 0);
  150.     }
  151.     
  152.     // ----- Draw each color -----
  153.     FW_CRectShape rect(FW_CRect(ff(3), ff(3), ff(11), ff(11)));
  154.     short colorIndex = 0;
  155.     FW_CColor color;
  156.     FW_CMemoryManager::LockSystemHandle((FW_PlatformHandle)fColorTable);
  157.     for (short j = 0; j<8; j++)
  158.     {
  159.         for (short i = 0; i<32; i++)
  160.         {
  161.             GetColor(colorIndex, &color);
  162.             rect->SetForeColor(color);
  163.             rect->Draw(gc);
  164.             rect->MoveShape(ff(9), ff(0));
  165.             colorIndex++;
  166.         }
  167.         
  168.         rect->MoveShape(ff(-9 * 32), ff(9));
  169.     }
  170.     FW_CMemoryManager::UnlockSystemHandle((FW_PlatformHandle)fColorTable);
  171. }
  172.  
  173. //------------------------------------------------------------------------------
  174. // CPaletteFacet::DoMouseDown
  175. //------------------------------------------------------------------------------
  176.  
  177. FW_Boolean CPaletteFacet::DoMouseDown(const FW_CPoint& where, XMPEventData event)
  178. {
  179. FW_UNUSED(event);
  180. FW_UNUSED(where);
  181.  
  182.     short colorIndex = CalcIndex(where);
  183.     
  184.     if (colorIndex>=0)
  185.     {
  186.         FW_CColor color;
  187.         GetColor(colorIndex, &color);
  188.         
  189.         if (::TestOptionKey(event))
  190.             fDrawPart->SetPenColor(color);
  191.         else
  192.             fDrawPart->SetFillColor(color);    
  193.     }
  194.  
  195.     return TRUE;
  196. }
  197.  
  198. //------------------------------------------------------------------------------
  199. // CPaletteFacet::GetColor
  200. //------------------------------------------------------------------------------
  201.  
  202. void CPaletteFacet::GetColor(short colorIndex, FW_CColor* color) const
  203. {
  204.     *color = (*fColorTable)->ctTable[colorIndex].rgb;
  205. }
  206.  
  207. //------------------------------------------------------------------------------
  208. // CPaletteFacet::CalcIndex
  209. //------------------------------------------------------------------------------
  210.  
  211. short CPaletteFacet::CalcIndex(const FW_CPoint& where) const
  212. {
  213.     FW_CRect paletteRect(ff(2), ff(2), ff(kPaletteWindowWidth-2), ff(kPaletteWindowHeight-2));
  214.     
  215.     if (paletteRect.Contains(where))
  216.     {
  217.         short x = (FixedToInt(where.x) - 2) / 9;
  218.         short y = (FixedToInt(where.y) - 2) / 9;        
  219.         return y * 32 + x;
  220.     }
  221.     
  222.     return -1;
  223. }